﻿using NDF.Web.Mvc;
using NDF.Web.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using UA.Data;
using UA.Data.Model;
using UA.Framework;
using UA.Framework.Authorization;
using UA.Framework.Views;
using UA.Web.Annotations;

namespace UA.Web.Controllers
{
    public class AccountController : Controller
    {

        /// <summary>
        /// 个人信息管理页面
        /// </summary>
        /// <returns></returns>
        [Module("01")]
        public ActionResult Manage()
        {
            return this.View(UAAuthentication.UserToken.User);
        }


        /// <summary>
        /// 修改当前登录用户的个人信息。
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        [Module("01")]
        public ActionResult Manage(User user)
        {
            if (!this.ModelState.IsValid)
            {
                this.ViewBag.Message = "您输入的信息不正确，请重新输入！";
                return View();
            }
            var entity = new { Key = UAAuthentication.UserToken.UserKey.Value, Code = user.Code, Name = user.Name, Sex = user.Sex, Birthday = user.Birthday };
            var ret = new UserAccessor().Edit(entity);
            if (ret > 0)
            {
                UAAuthentication.RefreshUserToken();
                this.ViewBag.Message = "个人信息修改成功！";
                return this.View();
            }
            else
            {
                this.ViewBag.Message = "个人信息修改失败！";
                return View();
            }
        }




        [Module("02")]
        public ActionResult UpdatePwd()
        {
            return this.View(UAAuthentication.UserToken.User.ToView<UpdatePwdView>());
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        [Module("02")]
        public ActionResult UpdatePwd(UpdatePwdView view)
        {
            if (!this.ModelState.IsValid)
            {
                this.ViewBag.Message = "您输入的信息不正确，请重新输入！";
                return View();
            }
            if (string.IsNullOrWhiteSpace(view.Password))
            {
                this.ViewBag.Message = "请输入原密码！";
                return View();
            }
            if (string.IsNullOrWhiteSpace(view.NewPassword))
            {
                this.ViewBag.Message = "请输入新密码！";
                return View();
            }
            if (!view.NewPassword.Equals(view.NewPasswordConfirm))
            {
                this.ViewBag.Message = "两次输入的新密码不一致，请重新输入！";
                return View();
            }
            if (view.Password.Equals(view.NewPassword))
            {
                this.ViewBag.Message = "新旧密码不能一样，请重新输入！";
                return View();
            }
            if (!UAAuthentication.Authenticate(UAAuthentication.UserToken.UserCode, view.Password))
            {
                this.ViewBag.Message = "旧密码错误，请重新输入！";
                return View();
            }

            var entity = new { Key = UAAuthentication.UserToken.UserKey.Value, Password = view.NewPassword };
            var ret = new UserAccessor().Edit(entity);
            if (ret > 0)
            {
                UAAuthentication.RefreshUserToken();
                this.ViewBag.Message = "密码修改成功！";
                return this.View();
            }
            else
            {
                this.ViewBag.Message = "密码修改失败！";
                return View();
            }
        }




    }
}
